package org.crag.cafca.gui.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Retrieve all the SourceType in DB and create a JSONObjet in order to populate the SourceType Combobox.  
 *
 */
@WebServlet("/SourceTypeServlet")
public class SourceTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public SourceTypeServlet() {
        super();
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		JSONObject toSendBack = new JSONObject(); 
		JSONArray dataArray = new JSONArray(); 
		Collection<JSONObject> items = new ArrayList<JSONObject>(); 
		
		JSONObject row = new JSONObject(); 
		row.put("sourcetype", "CAR"); 
		row.put("description", "New car"); 
		dataArray.put(row); 

		row = new JSONObject(); 
		row.put("sourcetype", "ELECTRICAL COUNTER"); 
		row.put("description", "New electrical counter"); 
		dataArray.put(row); 
		
		toSendBack.put("data", dataArray);
		toSendBack.put("success", true); 
		response.getWriter().print(toSendBack.toString());
		response.setStatus(HttpServletResponse.SC_OK);
	}

}
